
import { login, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'

const state = {
  token: getToken() || '',
  userInfo: {}
}
const mutations = {
  setToken(state, newToken) {
    state.token = newToken
    setToken(newToken)
  },
  removeToken() {
    state.token = ''
    removeToken()
  },
  updateUserInfo(state, info) {
    state.userInfo = info
  }
}
const actions = {
  async loginAction(context, data) {
    const res = await login(data)
    context.commit('setToken', res.token)
  },
  async getInfoAction(context) {
    const res = await getInfo()
    context.commit('updateUserInfo', res.userInfo)
  },
  logoutAction(context) {
    context.commit('removeToken')
    context.commit('updateUserInfo', {})
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

